package io.github.wslxm.springbootplus2.utils;

import io.github.wslxm.springbootplus2.client.sys.model.vo.USysConfigVO;
import io.github.wslxm.springbootplus2.core.constant.BooleanConst;
import io.github.wslxm.springbootplus2.core.utils.bean.XjSpringContextUtil;
import io.github.wslxm.springbootplus2.manage.sys.model.entity.SysAuthority;
import io.github.wslxm.springbootplus2.manage.sys.model.vo.SysBlacklistVO;
import io.github.wslxm.springbootplus2.manage.sys.service.SysBlacklistService;
import io.github.wslxm.springbootplus2.manage.sys.service.impl.SysAuthorityServiceImpl;
import io.github.wslxm.springbootplus2.manage.sys.service.impl.SysBlacklistServiceImpl;
import io.github.wslxm.springbootplus2.manage.sys.service.impl.SysConfigServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 系统 缓存缓存对象获取 类
 * <P>
 *     系统缓存对象 --  缓存key  CacheKey
 * </P>
 *
 * @author wangsong
 * @email 1720696548@qq.com
 * @date 2022/3/23 14:36
 */
public class XjCacheUtil {

    /**
     * 获取黑名单白名单 IP集 （通过type获取）
     *
     * @param type
     * @return List<IP>
     */
    public static List<String> findBlacklistByType(Integer type) {
        // 获取bean
        String beanName = toLowerCaseFirstOne(SysBlacklistServiceImpl.class.getSimpleName());
        SysBlacklistService bean = (SysBlacklistService) XjSpringContextUtil.getBean(beanName);
        // 调用方法处理数据
        List<SysBlacklistVO> blacklistVos = bean.listByType(type);
        if (!blacklistVos.isEmpty()) {
            return blacklistVos.stream().map(SysBlacklistVO::getIp).collect(Collectors.toList());
        }
        return new ArrayList<>(0);
    }

    /**
     * 获取全局配置 （根据code查询）
     *
     * @param code
     * @return
     */
    public static USysConfigVO findConfigByCode(String code) {
        // 获取bean
        String beanName = toLowerCaseFirstOne(SysConfigServiceImpl.class.getSimpleName());
        SysConfigServiceImpl bean = (SysConfigServiceImpl) XjSpringContextUtil.getBean(beanName);
        // 调用方法处理数据
        return bean.findByCode(code);
    }

    /**
     * 获取全局配置 （根据code查询）, 针对布尔参数
     *
     * @param code
     * @return 数据不存在或内容不为true, 都将返回false
     */
    public static boolean findConfigBooleanByCode(String code) {
        // 获取bean
        String beanName = toLowerCaseFirstOne(SysConfigServiceImpl.class.getSimpleName());
        SysConfigServiceImpl bean = (SysConfigServiceImpl) XjSpringContextUtil.getBean(beanName);
        // 调用方法处理数据
        USysConfigVO configVO = bean.findByCode(code);
        if (configVO == null) {
            return false;
        }
        return BooleanConst.TRUE_STE.equals(configVO.getContent());
    }



    /**
     * 查询系统的所有接口权限数据
     *
     * @return
     */
    public static Map<String, SysAuthority> findAuthAllToMap() {
        String beanName = toLowerCaseFirstOne(SysAuthorityServiceImpl.class.getSimpleName());
        SysAuthorityServiceImpl bean = (SysAuthorityServiceImpl) XjSpringContextUtil.getBean(beanName);
        return bean.findListAllToMap();
    }


    /**
     * 首字母转小写
     *
     * @author wangsong
     * @email 1720696548@qq.com
     * @date 2022/3/23 15:21
     */
    private static String toLowerCaseFirstOne(String str) {
        if (Character.isLowerCase(str.charAt(0))) {
            return str;
        } else {
            return Character.toLowerCase(str.charAt(0)) + str.substring(1);
        }
    }
}
